package com.itheima.controller;

import com.itheima.pojo.Employee;
import com.itheima.pojo.Result;
import com.itheima.service.EmployeeService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Log4j2
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private RedisTemplate redisTemplate;




    /**
     *登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody Employee employee) {
        Result login = employeeService.login(employee);
        System.out.println("login = " + login);
        return login;
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result  logout(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        redisTemplate.delete(token);
        return new Result(1,null,"{}");
    }


    /**
     * 分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result page(String name,
                       @RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize){
        return  employeeService.page(name,page,pageSize);
    }

    /**
     * 根据id查询员工
     */
    @GetMapping("/{id}")
    public Result getid(@PathVariable String id){
    return employeeService.getid(id);
    }

    /**
     * 新增员工
     */
    @PostMapping
    public Result add(HttpServletRequest request,@RequestBody Employee employee) {
        Result Result = employeeService.add(request,employee);
        return Result;
    }

    /**
     * 修改员工
     */
    @PutMapping
    public Result update(@RequestBody Employee employee){
        return employeeService.put(employee);
    }
}


